package de.unidue.langtech.teaching.pp.example;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import ude.DetectedTweetAtmosphere;
import ude.RealTweetAtmosphere;

/**
 * Gives an general and specific overview of accuracy and missclassifications
 * 
 * @author Marco
 *
 */

public class EvaluatorTwitter
    extends JCasAnnotator_ImplBase
{

    private int correct;
    private int nrOfDocuments;
    
    //these are for the wrong detected
    private int positive, negative, neutral, positiveAsNeutral, positiveAsNegative, 
    			negativeAsPositive, negativeAsNeutral, neutralAsPositive, neutralAsNegative;
    
    /* 
     * This is called BEFORE any documents are processed.
     */
    @Override
    public void initialize(UimaContext context)
        throws ResourceInitializationException
    {
        super.initialize(context);
        correct = 0;
        nrOfDocuments = 0;
    }
    
    
    /* 
     * This is called ONCE for each document
     */
    @Override
    public void process(JCas jcas)
        throws AnalysisEngineProcessException
    {
        nrOfDocuments++; 
        
        DetectedTweetAtmosphere detectedAtmosphere = JCasUtil.selectSingle(jcas, DetectedTweetAtmosphere.class);
        RealTweetAtmosphere realAtmosphere = JCasUtil.selectSingle(jcas, RealTweetAtmosphere.class);

        
     //   System.out.println(realAtmosphere.getAtmosphere() + " detected as " + detectedAtmosphere.getAtmosphere());
     
        // if correct detected -> count it
        if (detectedAtmosphere.getAtmosphere().equals(realAtmosphere.getAtmosphere())
        		|| detectedAtmosphere.getAtmosphere().equals("\"neutral\"") && realAtmosphere.getAtmosphere().equals("\"objective-OR-neutral\"")
        		|| detectedAtmosphere.getAtmosphere().equals("\"neutral\"") && realAtmosphere.getAtmosphere().equals("\"objective\"")) {
         correct++;

            
        }  // if wrong detected, count the original classifications and types of wrong ones
         else { 
        	 if (realAtmosphere.getAtmosphere().equals("\"positive\"")){
        		
        		 if (detectedAtmosphere.getAtmosphere().equals("\"neutral\"")) {
        			positiveAsNeutral++;
        		 } else { 
        			positiveAsNegative++;
        		 }
        		 positive++;
        }
        else if (realAtmosphere.getAtmosphere().equals("\"negative\"")) {
           		if (detectedAtmosphere.getAtmosphere().equals("\"neutral\"")) {
        			negativeAsNeutral++;
        		} else { 
        			negativeAsPositive++;
        		}
           		 negative++;
        } 
        else if (realAtmosphere.getAtmosphere().equals("\"neutral\"")) {
           		if (detectedAtmosphere.getAtmosphere().equals("\"positive\"")) {
        			neutralAsPositive++;
        		} else { 
        			neutralAsNegative++;
        		}
           		neutral++;
        }  
        else if (realAtmosphere.getAtmosphere().equals("\"objective-OR-neutral\"")) {
           		neutral++;
        }  
        else if (realAtmosphere.getAtmosphere().equals("\"objective\"")) {
           		neutral++;
        }
      }  
    }
    /* 
     * This is called AFTER all documents have been processed.
     */
    @Override
    public void collectionProcessComplete()
        throws AnalysisEngineProcessException
    {
        super.collectionProcessComplete();
        double accuracy = (double)correct / (double)nrOfDocuments *100;
       
        System.out.println("\n"+" accuracy: "+accuracy +"% - " +correct + " out of " + nrOfDocuments + " are classified correctly.");
        System.out.println("\nSummary: Wrong classified:");
        System.out.println("--------------------------");
        System.out.println(" positives: " + positive);
        System.out.println("\n negatives: " + negative);
        
        System.out.println("\n For the classifier, neutral, objective and objective or neutral are identical. Neutrals contain neutral + objective + objective or neutral.");
        System.out.println("\n neutrals: " + neutral);    
        System.out.println("-----------------------------");
        
        System.out.println("\n\n *******************************");        
        System.out.print(" Wrong classifications in Detail");
        System.out.print("\n\n gold \t \t classified as \t amount");
        System.out.print("\n --------------------------------------");
        System.out.print("\n neutral \t positive \t " + neutralAsPositive);
        System.out.print("\n neutral \t negative \t " + neutralAsNegative);
        System.out.print("\n --------------------------------------");
        System.out.print("\n positive \t negativ \t " + positiveAsNegative);
        System.out.print("\n positive \t neutral \t " + positiveAsNeutral);
        System.out.print("\n ---------------------------------------------");
        System.out.print("\n negative \t neutral \t " + negativeAsNeutral);
        System.out.print("\n negativ \t positiv \t " + negativeAsPositive);
   
    }
}